<!-- Ticket Add Page -->
<div class="content" id="tickets_add">
	<a href="#/tickets/list">Back</a>
	<h2>Create New Ticket</h2>
	<form onsubmit="return submitTicketsAdd();">
		<div>
			Project: <select name="project" onchange="loadReleases();"></select>
			Release: <select name="release"></select><br/>
			Type: <select name="type">
				<option value="defect">Defect</option>
				<option value="enhancement">Enhancement</option>
				<option value="task">Task</option>
			</select><br/>
		</div>
		<br/>

		<div>
			Name: <input type="text" name="name" style="width: 500px;"/><br/>
			Full Description<br/>
			<textarea name="description" cols="80" rows="15"></textarea>
		</div>

		<div class="require_employee">
			<h3>Ticket Properties</h3>
			Estimated Hours <input type="text" name="estimated_hours"/><br/>
			Owner: <select name="owner"></select><br/>
			Rate: <select name="rate">
				<option value="Standard">Standard</option>
				<option value="No Charge">No Charge</option>
				<option value="Premium">Premium</option>
			</select>
		</div>

		<div>
			<input type="submit" value="Create Ticket"/>
		</div>
	</form>
</div>
<script>
	function submitTicketsAdd(){
		env.models.Ticket.save(ticketsAddGetProps());
		alert("Ticket Saved");
		return false;
	}

	function ticketsAddGetProps(){
		var props = {}
		props['project'] = {id: $("div.content#tickets_add form select[@name='project']").val(), className: "vermouth.resources.project.Project"};
		var release_id = $("div.content#tickets_add form select[@name='release']").val();
		if(release_id){
			props['release'] = {id: release_id, className: "vermouth.resources.release.Release"};
		}
		props['type'] = $("div.content#tickets_add form select[@name='type']").val();
		props['name'] = $("div.content#tickets_add form input[@name='name']").val();
		props['description'] = $("div.content#tickets_add form textarea[@name='description']").val();
		if($.inArray("employee", env.user.auth_groups)){
			props['estimated_hours'] = $("div.content#tickets_add form input[@name='estimated_hours']").val();
			props['owner'] = {id: $("div.content#tickets_add form select[@name='owner']").val(), className: "boto_web.resources.user.User"};
			props['rate'] = $("div.content#tickets_add form select[@name='rate']").val();
		}
		return props;
	}

	function loadReleases(){
		var project_id = $("div.content#tickets_add form select[@name='project']").val();
		env.models.Release.find({project: project_id}, function(releases){
			$("div.content#tickets_add form select[@name='release']").empty();
			var opt = document.createElement("option");
			opt.appendChild(document.createTextNode("------------"));
			$("div.content#tickets_add form select[@name='release']").append(opt);
			for(var x=0; x < releases.length; x++){
				var release = releases[x];
				var opt = document.createElement("option");
				opt.value = release.id;
				opt.appendChild(document.createTextNode(release.props.name));
				$("div.content#tickets_add form select[@name='release']").append(opt);
			}
		});
	}

	function tickets_add_loader(){
		// Load Users
		if($.inArray("employee", env.user.auth_groups)){
			env.models.User.all(function(users){
				$("select[@name='owner']").empty();
				var opt = document.createElement("option");
				opt.appendChild(document.createTextNode("------------"));
				$("select[@name='owner']").append(opt);

				for(var x=0; x < users.length; x++){
					var user = users[x];
					var opt = document.createElement("option");
					opt.value = user.id;
					opt.appendChild(document.createTextNode(user.props.name));
					$("select[@name='owner']").append(opt);
				}
			});
		}
	}
	$("div.content#tickets_add")[0].loader = tickets_add_loader;
</script>
<!-- /Ticket Add page -->
